package com.zmx.controller;

import com.zmx.aop.MyLog;
import com.zmx.utils.RUtils;
import com.zmx.entity.SysMenu;
import com.zmx.entity.SysUser;
import com.zmx.service.SysMenuService;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * 2020/10/20
 * 10:40
 * zmx
 */
@RestController
public class SysMenuController {
    @Resource
    SysMenuService sysMenuService;

    @MyLog(value = "查询操作",description = "查询左侧菜单栏")
    @RequestMapping("sys/menu/user")
    public RUtils getUserMenu(){
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        List<SysMenu> sysMenus = sysMenuService.finAllMenu(sysUser.getUserId(), 0, 0);
        Set<String> perms = sysMenuService.finPermsByUid(sysUser.getUserId());
        return RUtils.success("获得成功",null).put("menuList",sysMenus).put("permissions",perms);
    }
    @MyLog(value = "查询操作",description = "分页查询所有菜单")
    @RequestMapping("/sys/menu/list")
    public RUtils getShowList(Integer limit, Integer offset, String search, String order){
        RUtils menuByPage = sysMenuService.findMenuByPage(limit, offset, search, order);
        return menuByPage;
    }
    @MyLog(value = "查询操作",description = "菜单整体结构查询")
    @RequestMapping("sys/menu/select")
    public RUtils getMenuSelect(){
        return sysMenuService.findAllTree();
    }
    @MyLog(value = "存储操作",description = "新菜单添加")
    @RequestMapping("sys/menu/save")
    public RUtils saveMenu(@RequestBody SysMenu sysMenu){
        return sysMenuService.saveMenu(sysMenu);
    }
    @MyLog(value = "更新操作",description = "菜单更新")
    @RequestMapping("sys/menu/info/{id}")
    public RUtils selectOneMenu(@PathVariable long id){
        System.out.println(id);
        return sysMenuService.selectByMenuId(id);
    }
    @RequestMapping("sys/menu/update")
    public RUtils updateMenu(@RequestBody SysMenu sysMenu){
        return sysMenuService.updateMenu(sysMenu);
    }
    /*假删除 标志字段，0使用 1 删除 而不真正的删除此段*/
    @MyLog(value = "删除操作",description = "假删除菜单")
    @RequestMapping("sys/menu/del")
    public RUtils deleteFake(@RequestBody List<Long> ids){
        System.out.println(ids);
        return sysMenuService.deleteFake(ids);
    }
}
